********************************************************************************
* set working directory
use FDI&Conflict.dta, clear

#delimit ;
global xmat1="lmrpe llrgdpcap llpop llgrowthrate lpolity2 lloil_gas_cap elf85 
relfrac lmtnest ncontig coldwar t1_onset2 t2_onset2 t3_onset2"; 
#delimit cr

* gen s = the subsequent years of an ongoing conflict

gen s = incidencev414==1&onset2v414==0


********************************************************************************
*************** Table 1: Onset of Civil Conflict *******************************
********************************************************************************
*Model 1
probit onset2v414 cr_rfdicap $xmat1 if s==0, robust

*Model 2 : IV probit

ivprobit onset2v414 (cr_rfdicap=lwdist) $xmat1 if s==0, vce(r)
test lwdist

* Store coefs and variance-covariance matrix for simulation 
matrix V_m2=e(V)
matrix M_m2=e(b)

*** Model 3
ivprobit onset2v414 (cr_rfdicap=lwdist) $xmat1 llopen if s==0, vce(r)
test lwdist

* Model 4
ivprobit onset2v414 (cr_rfdicap=lwdist) $xmat1 weightedvote logaidgdp /// 
weightedalliance weightedcolony  if s==0, vce(r)
test lwdist

* Model 5
ivprobit onset2v414 (cr_rfdicap=lwdist) $xmat1 lwt_all_int  if s==0, vce(r)
test lwdist

* Model 6
ivprobit onset2v414 (cr_rfdicap=lwdist) $xmat1 if Oil==0&s==0, vce(r)
test lwdist

*Model 7
ivprobit onset2v414 (cr_nonprimary=lwdist) $xmat1 if s==0, vce(r)
test lwdist

********************************************************************************
************************** Simulate Coefficients *******************************
********************************************************************************
**Model 2 in Table 1
clear
drawnorm x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 ///
x18	x19	x20	x21	x22	x23	x24	x25	x26	x27	x28	x29	x30	x31	x32	x33	x34, ///
n(1000) cov(V_m2) means(M_m2) seed(123)
save sim_probit_m2.dta,replace

********************************************************************************
******************* Table 2: FDI and Market Concentration **********************
********************************************************************************
use FDI&Concentration.dta, clear

* Model 1
ivreg2 top4w_employees_m (cr_rfdicap_m=lwdist) mrpe_agri lrgdpo lgrowthrate ///
polity2 loil_gas_cap, gmm robust

* Model 2
ivreg2 log_hhi_employees_m (cr_rfdicap_m=lwdist) mrpe_agri lrgdpo lgrowthrate ///
polity2 loil_gas_cap, gmm robust


********************************************************************************
******************* Table 3: Market Concentration and Conflict *****************
********************************************************************************
use Concentration&Conflict.dta, clear

gen outliers=(ccode==750|ccode==775)
* Model 1
reg onset_fq top4w_employees_m llrgdpcap_m lmrpe llpop_m llgrowthrate_m ///
lloil_gas_cap if outliers==0, robust

* Model 2
reg onset_fq log_hhi_employees_m llrgdpcap_m lmrpe llpop_m llgrowthrate_m ///
lloil_gas_cap if outliers==0, robust


********************************************************************************
************ Table 4: FDI, State Capacity, & Conflict **************************
********************************************************************************
use "FDI&Conflict.dta", clear

#delimit ;
global xmat1="llrgdpcap llpop llgrowthrate lpolity2 lloil_gas_cap elf85 
relfrac lmtnest ncontig coldwar t1_onset2 t2_onset2 t3_onset2";
#delimit cr

gen s = incidencev414==1&onset2v414==0

**Model 1: rpe
gen capacity=lmrpe

reg cr_rfdicap lwdist capacity $xmat1 if s==0, robust
predict fdi_hat if e(sample), xb

foreach i of varlist lwdist capacity $xmat1   {
	gen `i'_instr=`i'*capacity
}

gen fdicapacity=cr_rfdicap*capacity

reg fdicapacity *_instr capacity if s==0, nocon 
predict fdicapacity_hat if e(sample), xb

bootstrap, rep(1000) seed(11011) strata(ccode): ///
 reg onset2v414 fdi_hat fdicapacity_hat capacity $xmat1 if s==0, robust

*Store coefs and variance-covariance matrix for simulation 
matrix V_rpe=e(V)
matrix M_rpe=e(b)

drop capacity fdi_hat fdicapacity fdicapacity_hat *_instr

***Model 2: Tax/GDP
gen capacity=lltax_m

reg cr_rfdicap lwdist capacity $xmat1 if s==0, robust
predict fdi_hat if e(sample), xb

foreach i of varlist lwdist capacity $xmat1  {
	gen `i'_instr=`i'*capacity
}
*
gen fdicapacity=cr_rfdicap*capacity

reg fdicapacity *_instr capacity if s==0, nocon 
predict fdicapacity_hat if e(sample), xb

bootstrap, rep(1000) seed(11011) strata(ccode): ///
 reg onset2v414 fdi_hat fdicapacity_hat capacity $xmat1 if s==0, robust

*Store coefs and variance-covariance matrix for simulation 
matrix V_taxgdp=e(V)
matrix M_taxgdp=e(b)

drop capacity fdi_hat fdicapacity fdicapacity_hat *_instr

**********Model 3: Primary School Enrollment
gen capacity=lenrollpri_m

reg cr_rfdicap lwdist capacity $xmat1 if s==0, robust
predict fdi_hat if e(sample), xb

foreach i of varlist lwdist capacity $xmat1   {
	gen `i'_instr=`i'*capacity
}
*
gen fdicapacity=cr_rfdicap*capacity

reg fdicapacity *_instr capacity if s==0, nocon 
predict fdicapacity_hat if e(sample), xb

bootstrap, rep(1000) seed(11011) strata(ccode): ///
 reg onset2v414 fdi_hat fdicapacity_hat capacity $xmat1 if s==0, robust

*Store coefs and variance-covariance matrix for simulation 
matrix V_pri=e(V)
matrix M_pri=e(b)

drop capacity fdi_hat fdicapacity fdicapacity_hat *_instr

********************************************************************************
************************** Simulate Coefficients *******************************
********************************************************************************
**Model 1 in Table 4
clear
drawnorm x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17, ///
 n(1000) cov(V_rpe) means(M_rpe) seed(123)
save sim_rpe.dta,replace

* Model 2 in Table 4
clear
drawnorm x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17, ///
 n(1000) cov(V_taxgdp) means(M_taxgdp) seed(123)
save sim_tax.dta,replace
 
* Model 3 in Table 4
clear
drawnorm x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17, ///
 n(1000) cov(V_pri) means(M_pri) seed(123)

save sim_pri.dta,replace

